package com.Tokiu.controller;


import com.Tokiu.common.R;
import com.Tokiu.dto.DishDto;
import com.Tokiu.entity.Dish;
import com.Tokiu.entity.Employee;
import com.Tokiu.service.DishFlavorService;
import com.Tokiu.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;


    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dto){
        log.info(dto.toString());
        dishService.saveWithFlavor(dto);
        return R.success("新增菜品成功");
    }


    /**
     * 修改菜品时回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        log.info("根据id查询菜品信息");
        DishDto dishdto = dishService.getByIdWithFlavor(id);

        if(dishdto!=null)
        {
            return  R.success(dishdto);
        }

        return  R.error("没查到对应信息");
    }

    /**
     * 修改菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dishService.updeteWithFlavor(dishDto);

        return R.success("菜品修改成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Dish::getSort);

        dishService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }
}
